﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using EasyIot.Model;
using EasyIot.Repository;

namespace EasyIot.Service
{
    public class OrganizeService : BaseService<OrganizeModel>
    { 
        public static dynamic GetListByFilter(OrganizeModel filter, PageInfo pageInfo)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Organize TreeGrid数据列表
        /// </summary>
        public static IEnumerable<OrganizeModel> GetOrganizeList()
        {
            return OrganizeRepository.GetOrganizeList();
        }

        /// <summary>
        /// Organize treeSelect数据列表
        /// </summary>
        public static IEnumerable<TreeSelect> GetOrganizeTreeSelect()
        {
            IEnumerable<OrganizeModel> organizeList = BaseRepository<OrganizeModel>.GetAll("Id,FullName,ParentId", "ORDER BY SortCode ASC");
            var rootOrganizeList = organizeList.Where(x => x.ParentId == 0).OrderBy(x => x.SortCode);
            List<TreeSelect> treeSelectList = new List<TreeSelect>();
            foreach (var item in rootOrganizeList)
            {
                TreeSelect tree = new TreeSelect
                {
                    id = item.Id,
                    name = item.FullName,
                    open = false
                };
                GetOrganizeChildren(treeSelectList, organizeList, tree, item.Id);
                treeSelectList.Add(tree);
            }
            return treeSelectList;
        }

        /// <summary>
        /// 递归遍历treeSelectList
        /// </summary>
        private static void GetOrganizeChildren(List<TreeSelect> treeSelectList, IEnumerable<OrganizeModel> organizeList, TreeSelect tree, int id)
        {
            var childOrganizeList = organizeList.Where(x => x.ParentId == id).OrderBy(x => x.SortCode);
            if (childOrganizeList != null && childOrganizeList.Count() > 0)
            {
                List<TreeSelect> _children = new List<TreeSelect>();
                foreach (var item in childOrganizeList)
                {
                    TreeSelect _tree = new TreeSelect
                    {
                        id = item.Id,
                        name = item.FullName,
                        open = false
                    };
                    _children.Add(_tree);
                    tree.children = _children;
                    GetOrganizeChildren(treeSelectList, organizeList, _tree, item.Id);
                }
            }
        }

        public static bool DeleteContactById(int id)
        {
            string param = "@id0"; string deleteWhere = "where Id in (" + param + ",";

            DynamicParameters dpar = new DynamicParameters(); dpar.Add(param, id);  int index = 1;

            string where = "where ParentId=@ParentId";
            IEnumerable<OrganizeModel> notes = null;

            while (null == notes || notes.Count() > 0)
            {
                notes = GetByWhere(where, new { ParentId = id });
                foreach (var note in notes)
                {
                    id = note.Id;

                    param = "@id" + index;
                    deleteWhere += param + ",";

                    dpar.Add(param, id); index++;
                }
            }  

            deleteWhere = deleteWhere.Substring(0, deleteWhere.Length - 1); deleteWhere += ")";

            return DeleteByWhere(deleteWhere, dpar); 
        }

        public static bool IsExistCode(string encode)
        {
            string where = "where EnCode=@EnCode";
            return GetByWhere(where, new { EnCode = encode }).Count() > 0 ? true : false;
        }
    }
}
